$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'orth/problemManage/list',
        datatype: "json",
        colModel: [
            { label: 'ID', name: 'id_', index: "id_", width: 35, key: true,hidden:true },
            { label: '标题', name: 'title', width: 75 ,sortable:false},
            { label: '类型', name: 'directionType', width: 80,sortable:false, formatter: function(value, options, row){
                 switch(value)
				 {
					 case "1":return "关节修复与重建";
                     case "2":return "关节翻修";
                     case "3":return "下肢畸形矫正";
                     case "4":return "骨与关节感染";
                     case "5":return "风湿关节病";
					 default: return "";
				 }
            }},
            { label: '更新时间', name: 'updateTime', width: 80,sortable:false}
        ],
        viewrecords: true,

        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    $('.form-horizontal').validationEngine('attach',{
        autoHidePrompt: true,
        autoHideDelay: 2000,
        maxErrorsPerField: 1
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            title: null
        },
        showList: true,
        title:null,
        problem:{
            status:0,
			type:8,
            directionType:'1'
        },
        items:[{text:'关节修复与重建',value:'1'},{text:'关节翻修',value:'2'},{text:'下肢畸形矫正',value:'3'},{text:'骨与关节感染',value:'4'},{text:'风湿关节病',value:'5'}]
    },
    methods: {
        query: function () {
            jQuery("#jqGrid").jqGrid('setGridParam',{page:1});
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.problem = {status:0,type:8,directionType:'1'};

        },
        update: function () {
            var userId = getSelectedRow();
            if(userId == null){
                return ;
            }

            vm.showList = false;
            vm.title = "修改";

            vm.getUser(userId);

        },
        del: function () {
            var userIds = getSelectedRows();
            if(userIds == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "orth/problemManage/delete",
                    contentType: "application/json",
                    data: JSON.stringify(userIds),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {


            if(!$(".form-horizontal").validationEngine("validate")) {
                $(".formError").show();
                return;
            }
            /*if(vm.validator()){
                return ;
            }*/
            var url = vm.problem.id_ == null ? "orth/problemManage/save" : "orth/problemManage/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.problem),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getUser: function(id){
            $.get(baseURL + "orth/problemManage/info/"+id, function(r){
                vm.problem = r.problem;
            });
        },
        getRoleList: function(){
            $.get(baseURL + "sys/role/select", function(r){
                vm.roleList = r.list;
            });
        },
        reload: function () {
            vm.showList = true;
            $(".formError").hide();
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'title': vm.q.title},
                page:page
            }).trigger("reloadGrid");
        },
        queryLoad: function () {
            vm.showList = true;
            $(".formError").hide();

            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'title': vm.q.title},
                page:1
            }).trigger("reloadGrid");
        },
        validator: function () {

        }
    }
});